package com.xm.myapplication.MVP.A.View;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.orhanobut.logger.Logger;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.xm.httpapi.BaseMyView.IosAlertDialog;
import com.xm.httpapi.BaseView.BaseMVPFragment;
import com.xm.myapplication.MVP.A.IDemo;
import com.xm.myapplication.MVP.A.Model.LoginResult;
import com.xm.myapplication.MVP.A.Model.PwdLoginRequest;
import com.xm.myapplication.MVP.A.Presenter.FragmentPresenter;
import com.xm.myapplication.R;

import androidx.annotation.NonNull;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.OnNeverAskAgain;
import permissions.dispatcher.RuntimePermissions;

@RuntimePermissions
public class HomeMVPFragment extends BaseMVPFragment<FragmentPresenter, HomeMVPFragment> implements IDemo.IView {

    @BindView(R.id.btn_goto)
    Button btn_goto;


    @Override
    protected int getLayoutId() {
        return R.layout.fragment1;
    }

    @Override
    protected void initData(View view, Bundle savedInstanceState) {
        ButterKnife.bind(this, view);
        PwdLoginRequest pwdLoginRequest = new PwdLoginRequest("18502189235", "yf123456");
        presenter.load(pwdLoginRequest, getActivity());
        Logger.e("debug111111");
    }

    @Override
    public void setData(LoginResult loginResult) {
        toast("成功");
    }

    @Override
    public void error(String error) {
        toast("错误:" + error);
    }

    @OnClick(R.id.btn_goto)
    public void gotoPage() {
        intent(MVPActivity.class);
    }

    @OnClick(R.id.btn_callphone)
    public void btnCallPhone() {
        HomeMVPFragmentPermissionsDispatcher.callPhoneWithCheck(this);
    }


    @OnClick(R.id.btn_qqshare)
    public void qqshare() {
        UMImage image = new UMImage(getActivity(), R.mipmap.ic_launcher);//分享图标
        final UMWeb web = new UMWeb("http://www.baidu.com"); //切记切记 这里分享的链接必须是http开头
        web.setTitle("你要分享内容的标题");//标题
        web.setThumb(image);  //缩略图
        web.setDescription("你要分享内容的描述");//描述

        new ShareAction(getActivity()).setPlatform(SHARE_MEDIA.QQ)
                .withMedia(web)
                .setCallback(umShareListener)
                .share();
    }

    private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onStart(SHARE_MEDIA platform) {
            // 分享开始的回调
        }

        @Override
        public void onResult(SHARE_MEDIA platform) {
            Toast.makeText(getActivity(), platform + " 分享成功啦", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Toast.makeText(getActivity(), platform + " 分享失败啦", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            Toast.makeText(getActivity(), platform + " 分享取消了", Toast.LENGTH_SHORT).show();
        }
    };

    @NeedsPermission(Manifest.permission.CALL_PHONE)
    void callPhone() {
        Intent intent = new Intent(Intent.ACTION_CALL);
        Uri data = Uri.parse("tel:" + "18502189235");
        intent.setData(data);
        startActivity(intent);
    }

    //    @OnShowRationale(Manifest.permission.CALL_PHONE)
//    void showWhy(final PermissionRequest permissionRequest) {
//        permissionRequest.proceed();
//    }

//    @OnPermissionDenied(Manifest.permission.CALL_PHONE)
//    void showDenied() {
//        toast("应用权限被拒绝");
//    }

    @OnNeverAskAgain(Manifest.permission.CALL_PHONE)
    void onNeverAskAgain() {
        new IosAlertDialog(getActivity()).builder()
                .setMsg("应用权限被拒绝,为了不影响您的正常使用，请在 权限 中开启对应权限")
                .setPositiveButton("授权", v -> {
                    Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                    Uri uri = Uri.fromParts("package", getActivity().getPackageName(), null);
                    intent.setData(uri);
                    startActivity(intent);
                }).setNegativeButton("取消", v -> {
        }).show();
    }

    /**
     * 权限回调，调用PermissionsDispatcher的回调方法
     * 类名+PermissionsDispatcher哦
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        HomeMVPFragmentPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
    }

}
